package com.wiegand.util {
	import flash.events.NetStatusEvent;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;

	/**
	 * @author gandy
	 */
	
	public class SharedObjectManager {
		
		private static var _instance : SharedObjectManager;
		private var _soName : String;
		private var _so : SharedObject;

		public static function getInstance() : SharedObjectManager{
			if( _instance == null )
				_instance = new SharedObjectManager( new SingletonEnforecer() );
			return _instance;
		}
		
		public function SharedObjectManager( blocker:SingletonEnforecer ):void{
			if ( blocker == null ) {
            	throw new Error("SharedObjectManager::: Error: Instantiation failed: Use SharedObjectManager.getInstance() instead of new.");
          	}
          	else{}
		}
		
		public function initinalize(soName:String) : void{	
			_soName = soName;
			_so = SharedObject.getLocal(_soName);
		}
	
         public function saveValue(name:String, value:String):void {
            
            _so.data[name] = value;
            
            var flushStatus:String = null;
            
            try {
                flushStatus = _so.flush(10000);
            } catch (error:Error) {
//                trace("SharedObjectManager::: Error: Could not write SharedObject.");
            }
            
            if (flushStatus != null) {
                switch (flushStatus) {
                    case SharedObjectFlushStatus.PENDING:
//                        trace("SharedObjectManager:::saveValue Requesting permission to save object.");
                        _so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
                        break;
                    case SharedObjectFlushStatus.FLUSHED:
//                        trace("SharedObjectManager:::saveValue Value flushed to disk.");
                        break;
                }
            }
        }
        
        private function onFlushStatus(event:NetStatusEvent):void {
            switch (event.info.code) {
                case "SharedObject.Flush.Success":
//                    trace("SharedObjectManager:::onFlushStatus User granted permission -- value saved.");
                    break;
                case "SharedObject.Flush.Failed":
//                    trace("SharedObjectManager:::onFlushStatus User denied permission -- value not saved.");
                    break;
            }
            _so.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
        }
        
        public function clear():void {
            _so.clear();
            delete _so.data.savedValue;
        }
        
        public function getValue(name:String):String {
           	var value : String = null; 
           	try {
           	     value = _so.data[name];
           	 } catch (error:Error) {
//           	    trace("SharedObjectManager:::getValue Error: Could not get value from "+name+".");
           	 }
           	 return value;
        }	
			
	}
}

internal class SingletonEnforecer {}